require(["jquery","bbcare",
        "scprit/department/vm/assessmentScreening/bsid3.ui.js",
        "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js",
        "scprit/department/vm/assessmentScreening/bsid3.event.js"],
    function ($,$bb,$ui,$servers,$event) {
        var getParams = $bb.getPageParams();
        var type = getParams.type,userId = getParams.userId,answerId = getParams.answerId;
        //问卷题目获取：
        var paramsQuestion = $servers.getParams(type,userId);
        //患者信息展示：
        var infoParams = $servers.infoParamsBsid2(userId,type);
        var infoFn = function(dt){
            //基本信息渲染：
            dt = dt.data;
            var diff,diff2,answerResult,scoreObj = {};
            var tabsArr = [
                {"tabName":"认知", "quesType":"cognition"},
                {"tabName":"接受性交流", "quesType":"accept"},
                {"tabName":"表达性交流", "quesType":"expression"},
                {"tabName":"精细动作", "quesType":"fineaction"},
                {"tabName":"大动作", "quesType":"bigaction"},
                {"tabName":"行为观察目录", "quesType":"behavior"}
            ];
            if(answerId){
                answerResult = JSON.parse(getParams.answer.answerResult);
                scoreObj = answerResult.ques;
                tabsArr[0].isEnding = tabsArr[1].isEnding = tabsArr[2].isEnding =tabsArr[3].isEnding =tabsArr[4].isEnding = "end";
                tabsArr[0].start = answerResult.ques.start.cognition;
                tabsArr[1].start = answerResult.ques.start.accept;
                tabsArr[2].start = answerResult.ques.start.expression;
                tabsArr[3].start = answerResult.ques.start.fineaction;
                tabsArr[4].start = answerResult.ques.start.bigaction;
                tabsArr[0].end = answerResult.ques.end.cognition;
                tabsArr[1].end = answerResult.ques.end.accept;
                tabsArr[2].end = answerResult.ques.end.expression;
                tabsArr[3].end = answerResult.ques.end.fineaction;
                tabsArr[4].end = answerResult.ques.end.bigaction;
                tabsArr[0].score = answerResult.score.cogScore;
                tabsArr[1].score = answerResult.score.RCSocre;
                tabsArr[2].score = answerResult.score.ECSocre;
                tabsArr[3].score = answerResult.score.FMSocre;
                tabsArr[4].score = answerResult.score.GMSocre;
                dt.PatientInfo.careAgency = answerResult.careAgency;
                dt.PatientInfo.reason = answerResult.reason;
                dt.PatientInfo.testDate = getParams.testTime;
                diff2 = $servers.dateDiff(dt.PatientInfo.birthday,getParams.testTime);
            }else{
                scoreObj = {"accept":{},"bigaction":{},"cognition":{},"fineaction":{},"expression":{},"behavior":{},"start":{},"end":{},"startId":{},"endId":{}};
                diff2 = $servers.dateDiff(dt.PatientInfo.birthday,$servers.getNowFormatDate());
                dt.PatientInfo.testDate = $servers.getNowFormatDate();
            }
            var monthAge = $servers.getMonthAge(diff2,dt.PatientInfo.pregnancyDay);
            dt.PatientInfo.isPremature = monthAge.flag;
            dt.PatientInfo.monthAge = monthAge.monthAge;

            if(dt.Evaluation && dt.Evaluation.score){
                var lastTime = dt.Evaluation.testTime;
                diff = $servers.dateDiff(lastTime,$servers.getNowFormatDate());//计算上一次测试距离当前测试的天数；
                var score = JSON.parse(dt.Evaluation.score);
                tabsArr[0].devAge  = $event.intelligenceAge(diff,score.cogAge);
                tabsArr[1].devAge = $event.intelligenceAge(diff,score.RCSAge);
                tabsArr[2].devAge  = $event.intelligenceAge(diff,score.ECAge);
                tabsArr[3].devAge  = $event.intelligenceAge(diff,score.FMAge);
                tabsArr[4].devAge  = $event.intelligenceAge(diff,score.GMAge);
            }else{
                var dif= (dt.PatientInfo.pregnancyDay*1 < 37*7 && diff2 < 2*365) ? diff2-(280- dt.PatientInfo.pregnancyDay):diff2;
                tabsArr[0].devAge = tabsArr[1].devAge= tabsArr[2].devAge=tabsArr[3].devAge =tabsArr[4].devAge = dif;
            }

            $ui.basicInfoInit({"id":"#basic-info","data":dt.PatientInfo});
            $ui.selectTabInit({"id":"#testSelect","data":tabsArr});
            var quesFn = function(data){
                var dd = data.data;
                if(answerId){
                    dd = $event.historyDeal(data.data,scoreObj);
                    dd = $event.historyTabDeal(dd,scoreObj);
                }
                $event.tabClick(dd,scoreObj);
                $("#testSelect").find('div.item-label').first().click();
                if(!answerId){
                    $event.getScoreClick(scoreObj,userId,type);
                }else{
                    var viewResult =  function(){
                        $bb.openPage("bsidResult3",{
                            "userId":userId,
                            "answerId":answerId,
                            "type":type
                        });
                    };
                    var topbars = [
                        {
                            "icon":"fa fa-eye",
                            "name":"查看结果",
                            "callback":viewResult
                        }
                    ];
                    $bb.toolbar(topbars);
                }
            };
            $servers.getQuestion(paramsQuestion,quesFn);
        };
        $servers.getInfo(infoParams,infoFn);
    });
